<?php

/**
 * 购物车基于Cookie
 */
class CartAction extends CommonAction {
	protected $cart;
	protected $user_id;
	protected $condition;

	protected function _initialize() {
		parent::_initialize ();
		import ( '@.ORG.CartCookie' );
		$this->cart = new CartCookie ();
		$this->user_id = 2;
		$this->condition ['user_id'] = $this->user_id;
	}

	public function index() {
		$cartdata = $this->cart->readCart ();
		$this->assign ( 'cartdata', $cartdata );
		$this->display ();
	}
	
	// 添加物品
	public function insert() {
		$status = $this->cart->add ( 2, '燕子翩2014春季新款女装蕾丝修身显瘦连衣裙长袖弹力', 1, 100, array () );
		$this->assign ( 'status', $status );
		$this->display ();
	}
	
	// 更新物品
	public function update() {
		$status = $this->cart->update ( array (
				2,
				'燕子翩2014春季新款女装蕾丝修身显瘦连衣裙长袖弹力',
				4,
				100,
				array () 
		) );
		$this->assign ( 'status', $status );
		$this->display ();
	}
	
	// 清空物品
	public function clear() {
		$this->cart->clear ();
		$this->redirect ( U ( 'Cart/index' ) );
	}
	
	// 物品存入数据库
	public function savetodb() {
		$cartdata = $this->cart->readCart ();
		$cartContentString = serialize ( $cartdata );
		
		$cartdb = M ( 'cart' );
		$data = $cartdb->where ( $this->condition )->select ();
		dump ( $data );
		if ($data != NULL) {
			// 要修改的数据对象属性赋值
			$data ['cartdata'] = $cartContentString;
			$data ['updatetime'] = time ();
			$status = $cartdb->where ( $this->condition )->save ( $data );
		} else {
			// 要修改的数据对象属性赋值
			$data ['user_id'] = $this->user_id;
			$data ['cartdata'] = $cartContentString;
			$data ['updatetime'] = time ();
			$status = $cartdb->add ( $data );
		}
		
		$this->assign ( 'status', $status );
		$this->display ();
	}
	
	// 从数据库读取物品
	public function readfromdb() {
		$cartdb = M ( 'cart' );
		$data = $cartdb->where ( $this->condition )->find ();
		
		if ($data) {
			$cartArray = unserialize ( $data ['cartdata'] );
		}
		$this->assign ( 'cartArray', $cartArray );
		$this->display ();
	}
	
	// 清空数据库购物车
	public function clearfromdb() {
		$cartdb = M ( 'cart' );
		$data = $cartdb->where ( $this->condition )->find ();
		
		if ($data) {
			$data ['cartdata'] = '';
			$cartdb->where ( $this->condition )->save ( $data );
		}
	}
}